<template>
  <div
    style="
      height: 100%;
      width: 100%;
      background: url('../assets/img/background.jpg');
    "
  >
    <div class="ts"></div>

    <div class="loginForm">
      <el-form
        :model="loginForm"
        :rules="rules"
        ref="loginForm"
        label-width="130px"
        class="demo-ruleForm"
      >
        <el-form-item style="margin-left: 15px">
          <h1>校园互助平台</h1>
        </el-form-item>
        <el-form-item label="账号" prop="userName">
          <el-input
            v-model="loginForm.userName"
            style="width: 250px"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="userPass">
          <el-input
            type="password"
            v-model="loginForm.userPass"
            autocomplete="off"
            style="width: 250px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="getLogin('loginForm')"
            style="margin-left: 20px"
            >登录</el-button
          >
          <el-button @click="ToShouye()" style="margin-left: 80px"
            >取消</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      loginForm: {
        userName: "",
        userPass: "",
        userId: "",
      },

      rules: {
        userName: [
          { required: true, message: "请输入账户名称", trigger: "blur" },
        ],
        userPass: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    getLogin(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          axios({
            methods: "GET",
            params: {
              user: this.loginForm.userName, //属性名：账号
              pwd: this.loginForm.userPass,
            },
            url: "http://127.0.0.1:3000/login/loginUser",
          })
            .then((res) => {
              if (res.data.code == 200) {
                this.$router.push("/home/barter");
                console.log(res.data.data);
                window.sessionStorage.setItem(
                  "userName",
                  res.data.data[0].userName
                );
                window.sessionStorage.setItem(
                  "userId",
                  res.data.data[0].userId
                );
                window.sessionStorage.setItem(
                  "identity",
                  res.data.data[0].identity
                );
                 window.sessionStorage.setItem(
                  "userAudit",
                  res.data.data[0].userAudit
                );
              } else {
                this.$message.error("账号或密码有问题，请仔细核对");
                this.loginForm.userName = "";
                this.loginForm.userPass = "";
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    ToShouye() {
       this.$router.push("/home/barter");
    },
    submitForm(formName) {
      console.log(formName);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped lang="css">
.loginForm {
  width: 469px;
  box-shadow: 0 0 3px #ddd;
  background: #fff;
  padding: 20px 10px;
  position: fixed;
  /* right: 4%;
    top: 30%; */
  right: 36%;
  top: 34%;
  /* background: url("../assets/img/loginBackground.png"); */
  background-size: 400px;
  border-radius: 16px;
}
.ts {
  height: 100%;
  width: 100%;
  background: url("../assets/img/background.jpg") no-repeat;
  background-size: 100% 100%;
  opacity: 0.5;
}
</style>